﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class HeadSetting : BasePage
    {
        private Qs_HeadBLL qhb = new Qs_HeadBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            string whereStr = "";

            ViewState["Where"] = whereStr;

            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "";
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = qhb.GetRecordCount(Where);

            DataTable dtItem;

            dtItem = qhb.GetListByPage(Where, "CMY_ID", PageIng.CurrentPage, PageIng.RowsPerPage);

            if (dtItem != null && dtItem.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = null;
                grvShow.DataBind();
                PageIng.Visible = false;
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            //文本框关键字即为公司名称
            string whereStr = "";
            string com_name = txtKey.Text.Trim();
            if (!string.IsNullOrEmpty(com_name))
            {
                whereStr = " AND co.CMY_NAME like '%" + com_name + "%'";
            }
            ViewState["Where"] = whereStr;

            BindData(true);
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Response.Redirect("Add_HeadSetting.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    if (count == 1)
                    {
                        string pid = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;

                        string url = "Upd_HeadSetting.aspx?ID=" + pid;
                        Response.Redirect(url);
                        break;
                    }
                }
            }
        }

        protected void grvShow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "cu=style.backgroundColor;style.backgroundColor='#d5e7ff'");
                e.Row.Attributes.Add("onmouseout", "style.backgroundColor=cu");
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }
    }
}